//
//  YWAnchorContentTableViewCell.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWAnchorContentTableViewCell.h"
#import "YWAnchorContentModel.h"
#import "LTDataBase.h"
#import "LTAnchorModel.h"

@implementation YWAnchorContentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftImageView = [[UIImageView alloc] init];
        _leftImageView.tag = 1000;
        _leftImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_leftImageView];
        self.middleImageView = [[UIImageView alloc] init];
        _middleImageView.tag = 1001;
        _middleImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_middleImageView];
        self.rightImageView = [[UIImageView alloc] init];
        _rightImageView.tag = 1002;
        _rightImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_rightImageView];
        
        UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapPhoto:)];
        UITapGestureRecognizer *middleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapPhoto:)];
        UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapPhoto:)];
        [_leftImageView addGestureRecognizer:leftTap];
        [_middleImageView addGestureRecognizer:middleTap];
        [_rightImageView addGestureRecognizer:rightTap];
        
        self.leftNameLabel = [[UILabel alloc] init];
        _leftNameLabel.font = [UIFont systemFontOfSize:14];
        _leftNameLabel.textAlignment = 1;
        _leftNameLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_leftNameLabel];
        self.middleNameLabel = [[UILabel alloc] init];
        _middleNameLabel.font = [UIFont systemFontOfSize:14];
        _middleNameLabel.textAlignment = 1;
        _middleNameLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_middleNameLabel];
        self.rightNameLabel = [[UILabel alloc] init];
        _rightNameLabel.font = [UIFont systemFontOfSize:14];
        _rightNameLabel.textAlignment = 1;
        _rightNameLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_rightNameLabel];
        
        self.leftAttentionImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_leftAttentionImageView];
        self.leftAttentionImageView.userInteractionEnabled = YES;
        self.leftAttentionImageView.tag = 10000;
        UITapGestureRecognizer *leftaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedImage:)];
        [self.leftAttentionImageView addGestureRecognizer:leftaTap];
        
        self.middleAttentionImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_middleAttentionImageView];
        self.middleAttentionImageView.userInteractionEnabled = YES;
        self.middleAttentionImageView.tag = 10001;
        UITapGestureRecognizer *midaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedImage:)];
        [self.middleAttentionImageView addGestureRecognizer:midaTap];
        
        self.rightAttentionImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_rightAttentionImageView];
        self.rightAttentionImageView.userInteractionEnabled = YES;
        self.rightAttentionImageView.tag = 10002;
        UITapGestureRecognizer *rightaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedImage:)];
        [self.rightAttentionImageView addGestureRecognizer:rightaTap];
//        _leftAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
//        _middleAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
//        _rightAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
        [self addObserver:self forKeyPath:@"leftDid" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"midDid" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"rightDid" options:NSKeyValueObservingOptionNew context:nil];
        
        
    }
    return self;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"leftDid"];
    [self removeObserver:self forKeyPath:@"midDid"];
    [self removeObserver:self forKeyPath:@"rightDid"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"leftDid"]) {
        BOOL leftDid = [[change objectForKey:@"new"] boolValue];
        if (leftDid) {
           _leftAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse_sel"];
        }
        else
        {
            _leftAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
        }
    }
    if ([keyPath isEqualToString:@"midDid"]) {
        BOOL midDid = [[change objectForKey:@"new"] boolValue];
        if (midDid) {
            _middleAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse_sel"];
        }
        else
        {
            _middleAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
        }
    }
    if ([keyPath isEqualToString:@"rightDid"]) {
        BOOL rightDid = [[change objectForKey:@"new"] boolValue];
        if (rightDid) {
            _rightAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse_sel"];
        }
        else
        {
            _rightAttentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
        }
    }
}


- (void)didClickedImage:(UITapGestureRecognizer *)tap
{
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    NSInteger num = tap.view.tag - 10000;
    switch (num) {
        case 0:
        {
            if (_leftDid == NO) {
                [MBProgressHUD showSuccess:@"已关注"];
                self.leftDid = YES;
                YWAnchorContentModel *leftModel = self.listModel.list[0];
                LTAnchorModel *model = [[LTAnchorModel alloc] init];
                model.title = self.listModel.title;
                
                model.uid = leftModel.uid;
                model.nickname = leftModel.nickname;
                model.smallLogo = leftModel.smallLogo;
                [data insertDataWithAnchorModel:model];
            }
            else
            {
                
                [MBProgressHUD showSuccess:@"已取消关注"];
                self.leftDid = NO;
                YWAnchorContentModel *leftModel = self.listModel.list[0];
                [data delegateDataWithModel_id:leftModel.uid type:3];
            }
            
        }
            break;
        case 1:
        {
            if (_midDid == NO) {
                [MBProgressHUD showSuccess:@"已关注"];
                self.midDid = YES;
                YWAnchorContentModel *leftModel = self.listModel.list[1];
                LTAnchorModel *model = [[LTAnchorModel alloc] init];
                model.title = self.listModel.title;
                
                model.uid = leftModel.uid;
                model.nickname = leftModel.nickname;
                model.smallLogo = leftModel.smallLogo;
                [data insertDataWithAnchorModel:model];
            }
            else
            {
                
                [MBProgressHUD showSuccess:@"已取消关注"];
                self.midDid = NO;
                YWAnchorContentModel *leftModel = self.listModel.list[1];
                [data delegateDataWithModel_id:leftModel.uid type:3];
            }
        }
            break;
        case 2:
        {
            if (_rightDid == NO) {
                [MBProgressHUD showSuccess:@"已关注"];
                self.rightDid = YES;
                YWAnchorContentModel *leftModel = self.listModel.list[2];
                LTAnchorModel *model = [[LTAnchorModel alloc] init];
                model.title = self.listModel.title;
                
                model.uid = leftModel.uid;
                model.nickname = leftModel.nickname;
                model.smallLogo = leftModel.smallLogo;
                [data insertDataWithAnchorModel:model];
            }
            else
            {
                [MBProgressHUD showSuccess:@"已取消关注"];
                self.rightDid = NO;
                YWAnchorContentModel *leftModel = self.listModel.list[2];
                [data delegateDataWithModel_id:leftModel.uid type:3];
            }
        }
            break;
            
        default:
            break;
    }
}


- (void)didTapPhoto:(UITapGestureRecognizer *)tap
{
    NSInteger num = tap.view.tag - 1000;
    switch (num) {
        case 0:
        {
            NSInteger uidNum = [[_listModel.list objectAtIndex:0] uid];
            _uidBlock(uidNum);
        }
            break;
        case 1:
        {
            NSInteger uidNum = [[_listModel.list objectAtIndex:1] uid];
            _uidBlock(uidNum);
        }
            break;
        case 2:
        {
            NSInteger uidNum = [[_listModel.list objectAtIndex:2] uid];
            _uidBlock(uidNum);
        }
            break;
            
        default:
            break;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _leftImageView.frame = CGRectMake(10 * FITWIDTH, 10 * FITHEIGHT, (SCREENWIDTH - 40 * FITWIDTH) / 3, 110 * FITHEIGHT);
    _middleImageView.frame = CGRectMake(20 * FITWIDTH + _leftImageView.bounds.size.width, _leftImageView.frame.origin.y, _leftImageView.bounds.size.width, _leftImageView.bounds.size.width);
    _rightImageView.frame = CGRectMake(30 * FITWIDTH + _leftImageView.bounds.size.width * 2, _leftImageView.frame.origin.y, _leftImageView.bounds.size.width, _leftImageView.bounds.size.width);
    
    _leftNameLabel.frame = CGRectMake(_leftImageView.frame.origin.x, 130 * FITHEIGHT, _leftImageView.bounds.size.width, 20 * FITHEIGHT);
    _middleNameLabel.frame = CGRectMake(_middleImageView.frame.origin.x, 130 * FITHEIGHT, _leftImageView.bounds.size.width, 20 * FITHEIGHT);
    _rightNameLabel.frame = CGRectMake(_rightImageView.frame.origin.x, 130 * FITHEIGHT, _leftImageView.bounds.size.width, 20 * FITHEIGHT);
    
    _leftAttentionImageView.frame = CGRectMake(10 * FITWIDTH + (_leftImageView.bounds.size.width - 60 * FITWIDTH) / 2, 160 * FITHEIGHT, 60 * FITWIDTH, 25 * FITHEIGHT);
    _middleAttentionImageView.frame = CGRectMake(20 * FITWIDTH + (_leftImageView.bounds.size.width - 60 * FITWIDTH) / 2 * 3 + _leftAttentionImageView.bounds.size.width, 160 * FITHEIGHT, 60 * FITWIDTH, 25 * FITHEIGHT);
    _rightAttentionImageView.frame = CGRectMake(30 * FITWIDTH + (_leftImageView.bounds.size.width - 60 * FITWIDTH) / 2 * 5 + _leftAttentionImageView.bounds.size.width * 2, 160 * FITHEIGHT, 60 * FITWIDTH, 25 * FITHEIGHT);
}

- (void)setListModel:(YWAnchorListModel *)listModel
{
    if (_listModel != listModel) {
        _listModel = listModel;
        
        
        
    }
    YWAnchorContentModel *leftContainModel = [listModel.list objectAtIndex:0];
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:leftContainModel.smallLogo] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
    _leftNameLabel.text = leftContainModel.nickname;
    
    YWAnchorContentModel *middleContainModel = [listModel.list objectAtIndex:1];
    [_middleImageView sd_setImageWithURL:[NSURL URLWithString:middleContainModel.smallLogo] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
    _middleNameLabel.text = middleContainModel.nickname;
    
    YWAnchorContentModel *rightContainModel = [listModel.list objectAtIndex:2];
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:rightContainModel.smallLogo] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
    _rightNameLabel.text = rightContainModel.nickname;
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    NSInteger tempNum = [data isSaveDataInTableWithModel_id:leftContainModel.uid type:3];
    if (tempNum == 0) {
        self.leftDid = YES;
    }
    else
    {
        self.leftDid = NO;
    }
    tempNum = [data isSaveDataInTableWithModel_id:middleContainModel.uid type:3];
    if (tempNum == 0) {
        self.midDid = YES;
    }
    else
    {
        self.midDid = NO;
    }
    tempNum = [data isSaveDataInTableWithModel_id:rightContainModel.uid type:3];
    if (tempNum == 0) {
        self.rightDid = YES;
    }
    else
    {
        self.rightDid = NO;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
